<?php
namespace app\admin\controller;
use think\Controller;

Class Goods extends BaseAdmin
{

	//商品列表
    public function lst()
    {
    	
        //理解left join特点
        $GoodsRes=db('tp_goods')->alias('a')->field('a.*,b.cate_name,c.brand_name,SUM(d.goods_number) gn')
        ->join('tp_category b','a.category_id=b.id','LEFT')
        ->join('tp_brand c','a.brand_id=c.id','LEFT')
        ->join('tp_product d','a.id=d.goods_id','LEFT')
        ->group('a.id')
        ->order('a.id DESC')
        ->paginate(10);
         // dump($GoodsRes);die;

        //   $join = [
        //     ['tp_category c','g.category_id=c.id'],
        //     ['tp_brand b','g.brand_id=b.id','LEFT'],
        //     ['tp_type t','g.type_id=t.id','LEFT'],
        //     ['tp_product p','g.id=p.goods_id','LEFT'],
        // ];
        // $goodsRes=db('tp_goods')->alias('g')->field('g.*,c.cate_name,b.brand_name,t.type_name,SUM(p.goods_number) gn')->join($join)->group('g.id')->order('g.id DESC')->paginate(6);
        // dump($goodsRes);die;

        $this->assign(
            [
                'GoodsRes'=>$GoodsRes,
                
            ]);
         
        return $this->fetch('list');
    }

    //商品添加
    public function add()
    {   

        if(request()->isPost()){
        	$data=input('post.');
            // dump($data);die;
        

        	//验证
        	$validate = new \app\admin\validate\Goods;
        	if (!$validate->check($data)) {
        		$this->error($validate->getError());
        	}
        	
        	$add=model('tp_goods')->save($data);
        	if($add){
        		$this->success('保存商品成功','lst');
        	}
        	else{
        		$this->error('保存商品失败');
        	}

        }
        //分类信息
        $CateGoryRes=db('tp_category')->select();
        //无限极分类
        $CateGoryRes=$this->CateTree->catetree($CateGoryRes);
        // dump($CateGoryRes);die;
        //品牌信息
        $BrandRes=db('tp_brand')->field('id,brand_name')->select();
        //类型信息
        $TypeRes=db('tp_type')->select();
        //会员等级信息
        $MemberLevelRes=db('tp_member_level')->select();
        //推荐位信息
        $GoodsRecposRes=db('tp_recpos')->where(['rec_type'=>1])->select();

        // dump($GoodsRecposRes);die;
        $this->assign(
            [
                'MemberLevelRes'=>$MemberLevelRes,
                'TypeRes'=>$TypeRes,
                'BrandRes'=>$BrandRes,
                'CateGoryRes'=>$CateGoryRes,
                'GoodsRecposRes'=>$GoodsRecposRes

            ]
        );
        return $this->fetch();

    }

    //商品编辑
    public function edit($id)
    {
        if(request()->isPost()){
            $data=input('post.');
            // dump($data);die;
            
            //验证
            $validate = new \app\admin\validate\Goods;
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            
            $add=model('tp_goods')->update($data);
            if($add){
                $this->success('修改商品成功','lst');
            }
            else{
                $this->error('修改商品失败');
            }

        }

        $CateGoryRes=db('tp_category')->select();
        $CateGoryRes=$this->CateTree->catetree($CateGoryRes);
        $BrandRes=db('tp_brand')->field('id,brand_name')->select();
        $TypeRes=db('tp_type')->select();
        // dump($TypeRes);die;
        $MemberLevelRes=db('tp_member_level')->select();



        //商品基本信息
        $GoodsRes=db('tp_goods')->where(array('id'=>$id))->find();
        
        //会员价格
        $_MemberPriceRes=db('tp_member_price')->where(array('goods_id'=>$id))->select();
        //重新定义数组索引
        $MemberPriceRes=array();
        foreach ($_MemberPriceRes as $k => $v) {
            $MemberPriceRes[$v['mlevel_id']]=$v;
        }
        //商品相册
        $gphotoRes=db('tp_goods_photo')->where(array('goods_id'=>$id))->select();

        //商品所属类型
        $goodsTypeID=$GoodsRes['type_id'];
        $AttrRes=db('tp_attr')->where(array('type_id'=>$goodsTypeID))->select();
        // dump($AttrRes);die;

        //商品属性
        $_GoodsAttrRes=db('tp_goods_attr')->where(array('goods_id'=>$id))->select();
        $GoodsAttrRes=array();
        foreach ($_GoodsAttrRes as $k => $v) {
            $GoodsAttrRes[$v['attr_id']][]=$v;
        }
        //推荐位信息
        $GoodsRecposRes=db('tp_recpos')->where(['rec_type'=>1])->select();
        //商品推荐位内容
        $_myGoodsRecposRes=db('tp_rec_item')->where(['value_type'=>1,'value_id'=>$id])->select();
        foreach ($_myGoodsRecposRes as $k => $v) {
            $myGoodsRecposRes[]=$v['recpos_id'];
        }



        // dump($myGoodsRecposRes);die;


        
        $this->assign(
            [
                'MemberLevelRes'=>$MemberLevelRes,
                'TypeRes'=>$TypeRes,
                'BrandRes'=>$BrandRes,
                'CateGoryRes'=>$CateGoryRes,
                'GoodsRes'=>$GoodsRes,
                'MemberPriceRes'=>$MemberPriceRes,
                'gphotoRes'=>$gphotoRes,
                'AttrRes'=>$AttrRes,
                'GoodsAttrRes'=>$GoodsAttrRes,
                'GoodsRecposRes'=>$GoodsRecposRes,
                'myGoodsRecposRes'=>$myGoodsRecposRes

            ]
        );
        return $this->fetch();
    }



    //商品删除
    public function del($id)
    {
    	
        $tp=db('tp_goods');
        $thumb=$tp->field('or_thumb,sm_thumb,mid_thumb,big_thumb')->find($id);
        $del=$tp->delete($id);
        $img[]=IMG_UPLOADS.$thumb['or_thumb'];
        $img[]=IMG_UPLOADS.$thumb['sm_thumb'];
        $img[]=IMG_UPLOADS.$thumb['mid_thumb'];
        $img[]=IMG_UPLOADS.$thumb['big_thumb'];
        // dump($img);die;
        foreach ($img as $k => $v) {
            if(file_exists($v)){
            @unlink($v);
        }
        }
        if($del){
    		$this->success('删除商品成功','lst');

    	}else{
            $this->error('删除商品失败');
    	}
    	
        return $this->fetch();
    }


    //库存
    public function product($id){

        if(request()->isPost()){
            db('tp_product')->where(['goods_id'=>$id])->delete();
            $data=input('post.');
            // dump($data);die;
            $product=db('tp_product');
            $GoodsNumRes=$data['goods_num'];
            $GoodsAttrRes=$data['goods_attr'];
            foreach ($GoodsNumRes as $k => $v) {
                $strArr=array();
                foreach ($GoodsAttrRes as $k1 => $v1) {
                    //判断是否为空，不插入数据
                    if(intval($v1[$k])<=0){
                        continue 2;//跳出两层循环
                    }
                    $strArr[]=$v1[$k];
                }
                sort($strArr);
                $strArr=implode(',', $strArr);
                $product->insert(['goods_id'=>$id,'goods_number'=>$v,'goods_attr'=>$strArr]);
            }
            $this->success('添加库存成功！');
            return;


        }

        $_redioAttrRes=db('tp_goods_attr')->alias('a')->join('tp_attr b','a.attr_id=b.id')->field('a.*,b.attr_name')->where(array('goods_id'=>$id,'attr_type'=>1))->select();
        $redioAttrRes=array();
        foreach ($_redioAttrRes as $k => $v) {
            $redioAttrRes[$v['attr_name']][]=$v;
        }
        $GoodsProRes=db('tp_product')->where(array('goods_id'=>$id))->select();
        // dump($GoodsProRes);die;
        $this->assign(
            [
                'redioAttrRes'=>$redioAttrRes,
                'GoodsProRes'=>$GoodsProRes


        ]);
        // dump($redioAttrRes);die;

        return $this->fetch();
    }



 //ajax删除商品相册
    public function ajaxdelpic($id){
        $gphoto=db('tp_goods_photo');
        $gphotoRes=$gphoto->find();
        $ogPhoto=$gphotoRes['og_photo'];
        $bigPhoto=$gphotoRes['big_photo'];
        $midPhoto=$gphotoRes['mid_photo'];
        $smPhoto=$gphotoRes['sm_photo'];
        @unlink(IMG_UPLOADS.$ogPhoto);
        @unlink(IMG_UPLOADS.$bigPhoto);
        @unlink(IMG_UPLOADS.$midPhoto);
        @unlink(IMG_UPLOADS.$smPhoto);
        $del=$gphoto->where(array('id'=>$id))->delete();
        if($del){
            echo 1;
        }else{
            echo 2;
        }


    }

    
    


}